package ae.pcfc.cldos.online.common.facade.provider;

import org.springframework.transaction.annotation.Transactional;

import ae.pcfc.cldos.online.common.etksdb.domain.User;
import ae.pcfc.cldos.online.common.etksdb.domain.manager.UserDomainObjectManager;
import ae.pcfc.cldos.online.common.facade.BaseFacade;
import ae.pcfc.cldos.online.common.facade.CompanyEmployeeFacade;
import ae.pcfc.cldos.online.common.manager.GSEmployeeDetailsManager;
import ae.pcfc.cldos.online.common.tscdb.domain.manager.TTscClientCompanyDOM;
import ae.pcfc.cldos.online.common.vo.UserVO;
import ae.pcfc.cldos.online.net.service.vo.EmployeeDetailsFromGSVO;
import ae.pcfc.cldos.online.net.service.vo.ListOfEmployeeDetailsFromGSVO;

/**
 * 
 * @author arosa
 *
 */
public class CompanyEmployeeFacadeImpl extends BaseFacade implements CompanyEmployeeFacade {

	private UserDomainObjectManager userDomainObjectManager;
	
	public void setUserDomainObjectManager(
			UserDomainObjectManager userDomainObjectManager) {
		this.userDomainObjectManager = userDomainObjectManager;
	}
	
	private TTscClientCompanyDOM tTscClientCompanyDOM;
	
	public void settTscClientCompanyDOM(
			TTscClientCompanyDOM tTscClientCompanyDOM) {
		this.tTscClientCompanyDOM = tTscClientCompanyDOM;
	}
	
	private GSEmployeeDetailsManager gsEmployeeDetailsManager;
	
	public void setGsEmployeeDetailsManager(
			GSEmployeeDetailsManager gsEmployeeDetailsManager) {
		this.gsEmployeeDetailsManager = gsEmployeeDetailsManager;
	}
	
	@Transactional
	public ListOfEmployeeDetailsFromGSVO getEmployeDetailsList(UserVO userVO, Integer page, Integer perPage){
		return gsEmployeeDetailsManager.getEmployeeDetailsList(getCoLicNo(userVO), page, perPage);
	}
	
	@Transactional
	public ListOfEmployeeDetailsFromGSVO getVisitorsDetailsList(UserVO userVO, Integer page, Integer perPage){
		return gsEmployeeDetailsManager.getVisitorsDetailsList(getCoLicNo(userVO), page, perPage);
	}
	
	@Transactional
	public boolean validateEmployee(UserVO userVO, String employeeId){
		return gsEmployeeDetailsManager.validateEmployee(employeeId, getCoLicNo(userVO));
	}
	
	@Transactional
	public EmployeeDetailsFromGSVO getEmployeeDetailsById(UserVO userVO, String employeeId){
		EmployeeDetailsFromGSVO result = null;
		if(userVO.isAdmin() || userVO.isEmployee()){
			result = gsEmployeeDetailsManager.getEmployeeDetailsById(employeeId, getCoLicNo(userVO), false);
		} else {
			result = gsEmployeeDetailsManager.getEmployeeDetailsById(employeeId, null, true);
		}
		return result;
	}
	
	@Transactional
	public EmployeeDetailsFromGSVO getVisitorDetailsById(String visitorId, UserVO userVO){
		EmployeeDetailsFromGSVO result = null;
		if(userVO.isAdmin() || userVO.isEmployee()){
			result = gsEmployeeDetailsManager.getVisitorDetailsById(visitorId, getCoLicNo(userVO), false);
		} else {
			result = gsEmployeeDetailsManager.getVisitorDetailsById(visitorId, null, true);
		}
		return result;
	}
	
	private String getCoLicNo(UserVO userVO){
		User user = userDomainObjectManager.findUserAndClientByUserName(userVO.getUsername());
		String trakheesId = user.getClient().getCTrakheesId();
		return tTscClientCompanyDOM.getLicenseNumberNyTrakheesId(trakheesId);
	}
	
	
}
